import subprocess
import time
import sys
if __name__ == "__main__":
    tray_available = subprocess.run(["pgrep","-f","closetray.py"], stdout=subprocess.PIPE).stdout.decode('utf-8')
    print("close_num")
    print(len(tray_available.splitlines()))
    tray_available = len(tray_available.splitlines())
    if tray_available > 1:
        sys.exit(0)

    im_num = subprocess.run(["fcitx-remote"], stdout=subprocess.PIPE).stdout.decode('utf-8')[0]
    # with open('/usr/share/fcitx/Oyun/OyunUI/Oyun_index') as f:
    #     im_num = f.read()
    while True:
        im_now = subprocess.run(["fcitx-remote"], stdout=subprocess.PIPE).stdout.decode('utf-8')[0]
        print(im_now,im_num)
        if im_now != im_num:
            tray_available = subprocess.run(["pgrep","-f","trayWindow.py"], stdout=subprocess.PIPE).stdout.decode('utf-8').splitlines()
            for tray_id in tray_available:
                subprocess.run(["kill",tray_id])
            sys.exit()
            break
        time.sleep(2)